<template>
  <view class="demo-page">
    <view class="page-title">接口请求示例</view>
    
    <view class="btn-group">
      <button @click="fetchDemoList">获取列表数据</button>
      <button @click="fetchDemoDetail">获取详情</button>
    </view>
    
    <view class="result-area" v-if="resultData">
      <view class="result-title">返回结果:</view>
      <view class="result-content">{{ JSON.stringify(resultData, null, 2) }}</view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { useUserStore } from '@/store'
import { getDemoList, getDemoDetail } from '@/api'
const useUserStores = useUserStore()
const resultData = ref(null)

console.log('token1',useUserStores.token);
  useUserStores.setToken('123')
  console.log('token2',useUserStores.token);
// 获取列表数据
const fetchDemoList = async () => {

  try {
    uni.showLoading({ title: '加载中...' })
    const data = await getDemoList({ page: 1, limit: 10 })
    resultData.value = data
  } catch (error) {
    console.error('获取列表失败:', error)
  } finally {
    uni.hideLoading()
  }
}

// 获取详情
const fetchDemoDetail = async () => {
  try {
    uni.showLoading({ title: '加载中...' })
    // 这里使用1作为示例ID，实际应根据情况传入
    const data = await getDemoDetail(1)
    resultData.value = data
  } catch (error) {
    console.error('获取详情失败:', error)
  } finally {
    uni.hideLoading()
  }
}
</script>

<style scoped>
.demo-page {
  padding: 20rpx;
}

.page-title {
  font-size: 36rpx;
  font-weight: bold;
  margin: 30rpx 0;
  text-align: center;
}

.btn-group {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
  margin-bottom: 30rpx;
}

button {
  height: 80rpx;
  line-height: 80rpx;
  font-size: 32rpx;
}

.result-area {
  border: 1px solid #eee;
  border-radius: 10rpx;
  padding: 20rpx;
  margin-top: 20rpx;
}

.result-title {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.result-content {
  font-size: 24rpx;
  color: #333;
  white-space: pre-wrap;
  word-break: break-all;
}
</style>
